class Solution:
    def canCompleteCircuit(self, gas: list, cost: list) -> int:
        now_gas, min_gas, length = gas[0] - cost[0], gas[0] - cost[0], len(gas)
        gain_list = [gas[i] - cost[i] for i in range(length)]
        for i in range(1, length):
            now_gas += gain_list[i]
            min_gas = min(now_gas, min_gas)
        if now_gas < 0:
            return -1
        i = 0
        while min_gas < 0:
            i += 1
            min_gas -= gain_list[i-1]
        return i


a = Solution()
print(a.canCompleteCircuit(gas=[1, 2, 3, 4, 5], cost=[3, 4, 5, 1, 2]))  # 3
print(a.canCompleteCircuit(gas=[2, 3, 4], cost=[3, 4, 3]))  # -1
print(a.canCompleteCircuit([5, 8, 2, 8], [6, 5, 6, 6]))  # 3
